package homework;

import java.util.ArrayList;
import java.util.Scanner;



public class StudentTest {
	public static void main(String[] args) {
		StudentTest xs1 = new StudentTest();
		xs1.start();
	}
	
	Scanner sc = new Scanner(System.in);
	public void start() {
			while(true) {
		System.out.println("1、录入学生信息");
		System.out.println("2、按姓名查看信息");
		System.out.println("3、按专业查看");
		System.out.println("4、查看全部");
		System.out.println("请输入功能序号");
		switch(Integer.parseInt(sc.nextLine())) {
			case(1):
				input();
				break;
			case(2):
				getModelByName();
				break;
			case(3):
				getModelByZy();
				break;
			case(4):
				print();
				break;
		}
		System.out.println("***是否退出系统？y/n***");
		if("y".equalsIgnoreCase(sc.nextLine())) {
			break;
		}
	  }
		
	}	
	
	public void getModelByZy() {
		System.out.println("输入想要查看的专业");
		String zy = sc.nextLine();
		
		for(StudentModel model : list) {
			if(model.getZy().equals(zy)) {
				System.out.println("学号"+model.getId());
				System.out.println("姓名"+model.getName());
				System.out.println("专业"+model.getZy());
			}
		}
		
	}
	
	
	
	
	public void inputs() {
		while(true) {
			input();
			System.out.println("是否继续录入");
			if("n".equalsIgnoreCase(sc.nextLine())) {
				break;
			}
		}
	
	}
	
	public boolean isExist(int id) {
		for(StudentModel model : list) {
			if(model.getId() == id) {
				System.out.println("该学号("+ id +")存在");
				return true;
			}
		}
		return false;
	}
	
	
	    ArrayList<StudentModel> list  = new ArrayList<>();
		public void input() {
			
			StudentModel model = new StudentModel();
			int id = 0;
			do {
				System.out.println("输入学生学号");
				id = Integer.parseInt(sc.nextLine());
			}while(isExist(id));
			model .setId(id); //数据录完，放在对象当中的对应的属性上 
			System.out.println("请输入姓名");
			model.setName(sc.nextLine());
			System.out.println("请输入专业");
			model.setZy(sc.nextLine());
			
			list.add(model);
			
			
		}
		
		public void print() {
			for(StudentModel model : list) {
				System.out.println("学号"+model.getId());
				System.out.println("姓名"+model.getName());
				System.out.println("专业"+model.getZy());
				
			}
		}
		public void getModelByName() {
			System.out.println("输入想要查看的姓名");
			String name = sc.nextLine();
			for(StudentModel model : list) {
				if(model.getName().equals(name)) {
					System.out.println("学号"+model.getId());
					System.out.println("姓名"+model.getName());
					System.out.println("专业"+model.getZy());
					
					
				}	
			}
		
		}
		
	
}
